<?php
/**
 * 用户登录注册模块
 * User: Administrator
 * Date: 2018/11/1
 * Time: 16:18
 */

namespace app\index\controller;

use  library\Validator;
use exception\ResponsableException;
use module\SmsApi;
use module\UserApi;
use module\InsurerApi;
use library\Helpers;
use think\Db;
class  Login extends CommonController
{


    protected $_require_login = false;

    /**
     * 获取短信验证码
     */
    public function sendSms()
    {

        list($type, $phone) = Validator::make($this->request->request(), [
            'type' => 'required|between:1,4',
            'phone' => 'required|cellphone'
        ]);
        $tickets = '';
        switch ($type) {
            case 1: // 直接获取图形验证码
                break;
            case 2: // 获取注册短信验证码
                $tickets = SmsApi::send(SmsApi::SMS_TYPE_REGISTER, $phone);
                break;
            case 3: // 登录
                $tickets= SmsApi::send(SmsApi::SMS_TYPE_LOGIN, $phone);
                break;
            case 4: // 忘记密码
                SmsApi::send(SmsApi::SMS_TYPE_CHANGE_PASSWORD, $phone);
                break;
        }
        return $this->response->api(['code_id' => $tickets], self::SUCCESS_CODE);
    }

    /**
     * 验证短信验证码
     */
    private function checkSms($phone, $code, $tickets)
    {
        return SmsApi::smsCaptchaIsCorrect($phone, $code, $tickets);
    }

    /**
     * 注册
     */
    public function register()
    {
        list($phone, $code, $tickets) = Validator::make($this->request->request(), [
            'phone' => 'required|cellphone',
            'code' => 'required',
            'tickets' => 'required'
        ]);
        $info = $this->checkSms($phone, $code, $tickets);
        if (!$info) {
            return $this->response->api([], self::ERROR_CODE, '验证码错误');
        }
        $user_info = UserApi::getUserInfoByPhone($phone);
        if (!$user_info) {
            return $this->response->api([], self::ERROR_CODE, '用户不存在');
        }
        $data = [];
        $data['uid'] = $user_info['ui_id'];
        $data['username'] = Helpers::filterName($user_info['username']);
        $data['role_id'] = $user_info['role_id'];
        if ($user_info['role_id'] != UserApi::SALESMAN) {
            $data['mobile'] = $user_info['mobile'];
        }
        $data['card_num'] = $user_info['card_num']?Helpers::filterString($user_info['card_num'], 6, 4):'';
        if( $data['role_id']  == UserApi::INSURANT){
            $proInfo=Db::table('policy')->where('remer_id',$data['uid'])->find();
            $data['card_num']=$proInfo['policy_num']?$proInfo['policy_num']:'';
        }
        $data['company_name'] = $user_info['company_name'];
        //假如是物权人  显示保险人员信息
        if ($user_info['role_id'] == UserApi::INSURANT) {
            $c_info = InsurerApi::getUserbyRemerId($user_info['ui_id']);
            $data['c_company_name'] = $c_info['company_name'];
            $data['c_name'] = $c_info['username'];
            $data['c_mobile'] = $c_info['mobile'];
        }
        return $this->response->api($data, self::SUCCESS_CODE);
    }

    /**
     * 注册核对信息
     */
    public function checkInfo()
    {
        list($uid, $name, $card) = Validator::make($this->request->request(), [
            'uid' => 'required',
            'name' => 'required',
            'code' => '',
        ]);
        $user_info = UserApi::getUserInfoByWhere(['ui_id' => $uid, 'status' => 1]);
        if (!$user_info) {
            return $this->response->api([], self::ERROR_CODE, '用户不存在');
        }
        switch ($user_info['role_id']) {
            case UserApi::INSURER:///保险人员
                if (!$card) {
                    return $this->response->api([], self::ERROR_CODE, '参数错误');
                }
                if ($user_info['username'] != $name || $user_info['card_num'] != $card) {
                    return $this->response->api([], self::ERROR_CODE, '信息核对错误');
                }
                break;
            case UserApi::RRCORDOR://待录人
                if ($user_info['username'] != $name) {
                    return $this->response->api([], self::ERROR_CODE, '信息核对错误');
                }
                break;
            case UserApi::INSURANT://被保险人
                if (!$card) {
                    return $this->response->api([], self::ERROR_CODE, '参数错误');
                }
                $info = InsurerApi::getPolicyInfo(['*'], ['remer_id' => $uid, 'delete_ts' => 0]);
                if (!$info) {
                    return $this->response->api([], self::ERROR_CODE, '信息核对错误');
                }
                if ($user_info['username'] != $name || $info['policy_num'] != $card) {
                    return $this->response->api([], self::ERROR_CODE, '信息核对错误');
                }
                break;
            case UserApi::SALESMAN://业务员
                if (!$card) {
                    return $this->response->api([], self::ERROR_CODE, '参数错误');
                }
                if ($user_info['username'] != $name || $user_info['card_num'] != $card) {
                    return $this->response->api([], self::ERROR_CODE, '信息核对错误');
                }
                break;

        }
        //更改审核通过
        UserApi::updateUser(['ui_id' => $uid, 'status' => 1], ['status' => 2]);
        $token = $this->authorization($user_info['ui_id']);
        return $this->response->api(['token' => $token,'username'=>$user_info['username'],'img'=>$user_info['head_img']], self::SUCCESS_CODE, '信息核对成功');


    }

    /**
     * 登录
     */
    public function login()
    {
        list($phone, $code, $tickets) = Validator::make($this->request->request(), [
            'phone' => 'required|cellphone',
            'code' => 'required',
            'tickets' => 'required'
        ]);
        $info = $this->checkSms($phone, $code, $tickets);
        if (!$info) {
            return $this->response->api([], self::ERROR_CODE, '验证码错误');
        }
        $user_info = UserApi::getUserInfoByPhoneToSuss($phone);
        if (!$user_info) {
            return $this->response->api([], self::ERROR_CODE, '用户不存在');
        }
        $token = $this->authorization($user_info['ui_id']);
        return $this->response->api(['token' => $token,'username'=>$user_info['username'],'img'=>$user_info['head_img'],'role_id'=>$user_info['role_id']], self::SUCCESS_CODE, '登录成功');

    }
}